package com.aier.admin.entity.mapper;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;

import net.eicp.ghostboy.futil.StringUtil;

import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.jdbc.core.RowMapper;

import com.aier.admin.entity.SysKey;

public class SysKeyRowMapper implements RowMapper<SysKey> {
    @SuppressWarnings("unused")
    private int type;

    public static final int TYPE_UID = 10;

    public SysKeyRowMapper() {
    }

    public SysKeyRowMapper(int type) {
        this.type = type;
    }
    @Override
    public SysKey mapRow(ResultSet rs, int rowNum) throws SQLException {
        return parseSysKey(new SysKey(), rs);
    }

    public static SysKey parseSysKey(SysKey sk, ResultSet rs) throws SQLException {
        return parseSysKey(sk, rs, null);
    }

    public static SysKey parseSysKey(SysKey sk, ResultSet rs, String su)
        throws SQLException {

        if (StringUtil.isStringNull(su)) {
            su = "";
        } else {
            su += ".";
        }

        try {
            sk.setKeyid(rs.getLong(su + "keyid"));
        } catch (SQLException e) {
            sk.setKeyid(null);
        }
        try {
            sk.setType(rs.getShort(su + "type"));
        } catch (SQLException e) {
            sk.setType(null);
        }
        try {
            sk.setCdate(rs.getTimestamp(su + "cdate"));
        } catch (SQLException e) {
            sk.setCdate(null);
        }
        try {
            sk.setKvalue(rs.getString(su + "kvalue"));
        } catch (SQLException e) {
            sk.setKvalue(null);
        }
        try {
            sk.setAccount(rs.getString(su + "account"));
        } catch (SQLException e) {
            sk.setAccount(null);
        }

        return sk;
    }

    public static Map<String, Object> getPar(Map<String, Object> par, SysKey sk) {
        par.put("keyid",sk.getKeyid());
        par.put("type",sk.getType());
        par.put("cdate", sk.getCdate());
        par.put("kvalue", sk.getKvalue());
        par.put("account", sk.getAccount());
        return par;
    }

    public static Map<String, Object> getArgs(Map<String, Object> args, SysKey sk) {
        args.put("keyid",sk.getKeyid());
        return args;
    }

    /**
     * 
     * 
     * @param tfp
     * @return
     */
    public static PreparedStatementCreator getPreparedStatementCreator(
            final SysKey sk) {
        return new PreparedStatementCreator() {
            @Override
            public PreparedStatement createPreparedStatement(Connection con)
                throws SQLException {
                PreparedStatement ps = con.prepareStatement(parseSQL(sk),
                        new String[] { "keyid" });
                parsePS(ps, sk);
                return ps;
            }

        };
    }

    private static String parseSQL(SysKey sk) throws SQLException {
        StringBuffer sql = new StringBuffer("insert into sys_key(");
        int count = 0;
        if (null != sk.getKeyid()) {
            sql.append("keyid,");
            count++;
        }
        if (null != sk.getType()) {
            sql.append("type,");
            count++;
        }
        if (null != sk.getCdate()) {
            sql.append("cdate,");
            count++;
        }
        if (null != sk.getKvalue()) {
            sql.append("kvalue,");
            count++;
        }
        if (null != sk.getAccount()) {
            sql.append("account,");
            count++;
        }
        sql = new StringBuffer(sql.substring(0, sql.length() - 1));
        sql.append(") value(");
        for (int i = 0; i < (count - 1); i++) {
            sql.append("?,");
        }
        return sql.append("?)").toString();
    }

    private static void parsePS(PreparedStatement ps, SysKey sk)
        throws SQLException {
        int count = 1;
        if (null != sk.getKeyid()) {
            ps.setLong(count++, sk.getKeyid());
        }
        if (null != sk.getType()) {
            ps.setShort(count++, sk.getType());
        }
        if (null != sk.getCdate()) {
            ps.setTimestamp(count++,  sk.getCdate());
        }
        if (null != sk.getKvalue()) {
            ps.setString(count++, sk.getKvalue());
        }
        if (null != sk.getAccount()) {
            ps.setString(count++, sk.getAccount());
        }
    }

}
